<?php
class rechargeModel extends model{
	public $base;
	function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="recharge";	
	}
	
	public function pay_type(){
		 return array(
		 	"alipay"=>"支付宝",
			"tenpay"=>"财付通",
			"wxpay"=>"微信支付",
			"mibao"=>"密保卡",
			""=>"直接充值",
		 );
	}
	
	public function getUrl(){
		$order_id=get_post('order_id','i');
		if($order_id){
			$table=get_post("table",'h');
			$order_des="订单支付";
			switch($table){
				case "order":
						$table_order="order";
						$table_order_address="order_address";
					break;
				default:
						$table_order=$table."_order";
						$table_order_address=$table."_order_address";
					break;
			}
			$data=M($table_order)->selectRow(array("where"=>"order_id=".$order_id));
			if(empty($data)) $this->goall("参数出错",1);
			$data['table']=$table;
			$orderdata=base64_encode(json_encode($data));
			if($data['ispay']==2) $this->goall("该订单已支付",1);
			$backurl="/index.php/$table/order_id-".$order_id;
		}else{
			$backurl="/index.php/recharge/";
		}
		$pay_type=get_post('pay_type','h');
		$orderno="re".date("YmdHis").M("login")->userid;//根据实际情况一个用户1s不可能重复下订单
		$order_product=get_post('product_name')?get_post('product_name'):'网站充值';
		$order_price=$data?$data['money']:get_post('order_price',"r",2);
		$order_info=get_post('order_info','h');
		$bank_type=get_post('bank_type');
		/*****插入充值表******/
		M("recharge")->insert(array(
			"userid"=>$this->userid,
			"money"=>$order_price,
			"pay_type"=>$pay_type,
			"orderno"=>$orderno,
			"orderinfo"=>$order_product."<br>".$order_info, 
			"type_id"=>1,
			"tablename"=>$table?$table:"",
			"dateline"=>time(),
			"status"=>2,
			"siteid"=>SITEID,
			"orderdata"=>$orderdata,
		));
		
		/*插入充值表结束*/
		
		$url="http://".$_SERVER['HTTP_HOST']."/api/".$pay_type."/".$pay_type.".php";
		$url.="?orderno=$orderno";
		$url.="&bank_type=".$bank_type;
		$url.="&order_product=".urlencode($order_product);
		$url.="&order_price=".$order_price;
		$url.="&order_info=".urlencode($order_info);
		$url.="&backurl=".urlencode($backurl);
		switch($pay_type){
			case "wxpay":
					$url="/index.php?m=recharge_wxpay&a=go";
					$url.="&orderno=".$orderno;
					$url.="&bank_type=1";
					$url.="&order_product=".urlencode($order_product);
					$url.="&order_price=".$order_price;
					$url.="&order_info=".urlencode($order_info);
					return $url;
				break;
		}
		return $url;
	}
}
?>